from sqlalchemy import Column, Integer, String,ForeignKey
from sqlalchemy.orm import relationship

from src.models.base import TimeModel


class SpecialInterestGroup(TimeModel):
    __tablename__ = 'special_interest_group'

    id = Column(Integer, primary_key=True)
    name = Column(String(128), unique=True, comment='sig组名')

    repositories = relationship('Repository', back_populates='sig')

    def __str__(self):
        return self.name


class Repository(TimeModel):
    __tablename__ = 'repository'

    id = Column(Integer, primary_key=True)
    name = Column(String(128), unique=True, comment='仓名')
    sig_id = Column(Integer, ForeignKey('special_interest_group.id'), comment='所属sig组')
    owner_id = Column(Integer, ForeignKey('user.id'), comment='责任人')

    sig = relationship('SpecialInterestGroup', back_populates='repositories')
    owner = relationship('User', back_populates='repositories')
    repository_statuses = relationship('RespositoryStatus', back_populates='repo')
    issues = relationship('Issue', back_populates='repo')
    pull_requests = relationship('PullRequest', back_populates='repo')

    def __str__(self):
        return f'{self.name}-{self.owner.username}'